package myJs.user

import myJs.Utils._
import myJs.Implicits._

import myJs.myPkg.jquery._
import myJs.myPkg._
import org.scalajs.dom._
import org.scalajs.dom.ext.Ajax
import org.scalajs.dom.raw.{Blob, BlobPropertyBag}
import scalatags.Text.TypedTag
import scalatags.Text.all._
import shared.VarTool
import shared.plotly.Bar
import shared.plotly.element.Color.StringColor
import shared.plotly.element.{AxisType, HoverInfo, Marker, Orientation}
import shared.plotly.layout.HoverMode.Closest
import shared.plotly.layout.{Axis, Font, Layout, Margin}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.scalajs.js
import scala.scalajs.js.JSConverters._
import scala.scalajs.js.JSON
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import myJs.Implicits._
import myJs.tool.Tool
import scala.scalajs.js.Dynamic.{global => g}

/**
 * Created by yz on 2019/4/25
 */
@JSExportTopLevel("SampleSummary")
object SampleSummary {

  val typeTagEmpty = List[TypedTag[String]]()


  @JSExport("init")
  def init = {
    idChangeByValue(getId)
    $(".quick-nav .quick-nav-trigger").on("mouseenter", (y: Element, e: Event) => (
      $(y).parent().addClass("nav-is-visible")
      ))
    $("nav.quick-nav").on("mouseleave", (y: Element, e: Event) => (
      $(y).removeClass("nav-is-visible")
      ))

  }

  def idChangeByValue(myValue: String) = {
    val id = myValue
    val index = layer.load(1, Tool.layerOptions.shade(js.Array("0.1", "#fff")))
    refreshSampleInfo(id).map { x =>
      SampleDetail.showSampleDetail(id)
      layer.close(index)
    }
  }

  def getId = {
    g.id.toString
  }

  def refreshSampleInfo(id: String) = {
    val url = g.jsRoutes.controllers.SampleController.getSampleById().url.toString
    Ajax.get(url = s"${url}?id=${id}").map { xhr =>
      val dict = JSON.parse(xhr.responseText).toJDS
      dict.foreach { case (k, v) =>
        $(s"#sample #${k}").text(v.toString)
      }
      val downloadUrl = g.jsRoutes.controllers.SampleController.downloadReport().url.toString
      val downloadStr = a(title := "导出报告", href := s"${downloadUrl}?id=${dict("id")}", cursor.pointer,
        span(em(cls := "fa fa-download"))
      )
      $("#sample #export").html(downloadStr.render)
    }
  }

}
